package com.fbt.transfer.app.driver.ui.grab;

import android.content.Context;
import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.fbt.transfer.app.driver.http.AbstractCallback;
import com.fbt.transfer.app.driver.http.ApiClient;
import com.fbt.transfer.app.driver.http.ApiInterface;
import com.fbt.transfer.app.driver.model.GrabPageQuery;
import com.fbt.transfer.app.driver.model.PageInfo;
import com.fbt.transfer.app.driver.model.Result;
import com.fbt.transfer.app.driver.model.grab.WaitOrderUnit;
import com.fbt.transfer.app.driver.util.AppContextProvider;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;

public class GrabViewModel extends ViewModel {

  private static final String TAG = "GrabViewModel";
  private final ApiInterface apiInterface;
  private final MutableLiveData<List<WaitOrderUnit>> orderListLiveData = new MutableLiveData<>();
  private final MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
  private final MutableLiveData<String> errorLiveData = new MutableLiveData<>();
  private final MutableLiveData<Boolean> noMoreDataLiveData = new MutableLiveData<>();
  private final MutableLiveData<Boolean> emptyDataLiveData = new MutableLiveData<>();
  private int currentPage = 1;
  private boolean isLastPage = false;
  private List<WaitOrderUnit> allOrders = new ArrayList<>();
  private final Context context;

  public GrabViewModel () {
    apiInterface = ApiClient.getApiInterface();
    context = AppContextProvider.getAppContext();
  }

  public LiveData<List<WaitOrderUnit>> getOrderListLiveData () {
    return orderListLiveData;
  }

  public LiveData<Boolean> getLoadingLiveData () {
    return loadingLiveData;
  }

  public LiveData<String> getErrorLiveData () {
    return errorLiveData;
  }

  public LiveData<Boolean> getNoMoreDataLiveData () {
    return noMoreDataLiveData;
  }

  public LiveData<Boolean> getEmptyDataLiveData () {
    return emptyDataLiveData;
  }

  public boolean isLastPage () {
    return isLastPage;
  }

  public boolean isCurrentPageGreaterThanFirst () {
    return currentPage > 1;
  }

  public void loadFirstPage (String orderType, String serviceType, String appointmentTime) {
    currentPage = 1;
    isLastPage = false;
    allOrders.clear();
    loadingLiveData.setValue(true);
    errorLiveData.setValue(null);
    noMoreDataLiveData.setValue(false);
    emptyDataLiveData.setValue(false);

    GrabPageQuery query = new GrabPageQuery();
    query.setPageNum(currentPage);
    query.setPageSize(10);
    query.setOrderType(orderType);
    query.setServiceType(serviceType);
    query.setBookStart(appointmentTime);

    apiInterface.grabList(query).enqueue(new AbstractCallback<PageInfo<WaitOrderUnit>>(context, true) {
      @Override
      protected void onSuccess (PageInfo<WaitOrderUnit> pageInfo) {
        loadingLiveData.setValue(false);
        List<WaitOrderUnit> orders = pageInfo.getRecords();
        if (orders != null && !orders.isEmpty()) {
          allOrders.addAll(orders);
          orderListLiveData.setValue(new ArrayList<>(allOrders));
          isLastPage = !pageInfo.hasNext();
          if (isLastPage) {
            noMoreDataLiveData.setValue(true);
          }
        } else {
          emptyDataLiveData.setValue(true);
        }
      }

      @Override
      public void onFailure (Call<Result<PageInfo<WaitOrderUnit>>> call, Throwable t) {
        loadingLiveData.setValue(false);
        errorLiveData.setValue("网络错误: " + t.getMessage());
        Log.e(TAG, "Error loading first page", t);
      }
    });
  }

  public void loadNextPage (String orderType, String serviceType, String appointmentTime) {
    if (isLastPage) {
      return;
    }
    loadingLiveData.setValue(true);
    errorLiveData.setValue(null);

    GrabPageQuery query = new GrabPageQuery();
    query.setPageNum(currentPage + 1);
    query.setPageSize(10);
    query.setOrderType(orderType);
    query.setServiceType(serviceType);
    query.setBookStart(appointmentTime);

    apiInterface.grabList(query).enqueue(new AbstractCallback<PageInfo<WaitOrderUnit>>(context, true) {
      @Override
      protected void onSuccess (PageInfo<WaitOrderUnit> pageInfo) {
        loadingLiveData.setValue(false);
        List<WaitOrderUnit> orders = pageInfo.getRecords();
        if (orders != null && !orders.isEmpty()) {
          currentPage++;
          allOrders.addAll(orders);
          orderListLiveData.setValue(new ArrayList<>(allOrders));
          isLastPage = !pageInfo.hasNext();
          if (isLastPage) {
            noMoreDataLiveData.setValue(true);
          }
        } else {
          isLastPage = true;
          noMoreDataLiveData.setValue(true);
        }
      }

      @Override
      public void onFailure (Call<Result<PageInfo<WaitOrderUnit>>> call, Throwable t) {
        loadingLiveData.setValue(false);
        errorLiveData.setValue("网络错误: " + t.getMessage());
        Log.e(TAG, "Error loading next page", t);
      }
    });
  }
}
